package com.rritw.webtools.action.tool;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.HtmlUtils;

import com.rritw.webtools.action.IpsAction;
import com.rritw.webtools.common.Const;
import com.rritw.webtools.util.Base64;
import com.rritw.webtools.util.StringUtil;

@Controller
@Scope("prototype")
public class Base64Action extends IpsAction {

    private static final long   serialVersionUID = -7226283700786798920L;
    private static final Logger log              = Logger.getLogger(Base64Action.class);
    private String              content;
    private String              action;

    private String              contentResult;

    public String index() {
        super.init();
        return SUCCESS;
    }

    public String create() {
        try {
            super.init();
            if (StringUtil.isEmpty(content)) {
                return ERROR;
            }
            if ("encode".equals(action)) {
                contentResult = Base64.encode(content.getBytes(Const.CHARSET));
            } else {
                contentResult = new String(Base64.decode(content), Const.CHARSET);
            }
            if (!StringUtil.isEmpty(content)) {
                content = HtmlUtils.htmlEscape(content);
            }
            if (!StringUtil.isEmpty(contentResult)) {
                contentResult = HtmlUtils.htmlEscape(contentResult);
            }
        } catch (Exception e) {
            log.error("Format xml exception happened:" + e.getMessage(), e);
        }
        return SUCCESS;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContentResult() {
        return contentResult;
    }

    public void setAction(String action) {
        this.action = action;
    }

}
